package com.gdut.simple.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.data.entity.Post;
import com.gdut.simple.view.activity.explore.OtherUserInfoActivity;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.AbsViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

public class PostAdapter extends AbsRecyclerAdapter<Post> {

    public PostAdapter(Context context, List<Post> list) {
        super(context, list);
    }

    @Override
    protected AbsViewHolder createHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.layout_post_item, parent, false));
    }

    @Override
    protected void showViewHolder(AbsViewHolder holder, final int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (mData.get(position).getAuthor() != null) {
            viewHolder.author.setText(mData.get(position).getAuthor().getNickName());
            BmobFile profile = mData.get(position).getAuthor().getProfile();
            if (profile == null) {
                viewHolder.userProfile.setImageResource(R.drawable.ic_load_error);
            } else {
                String profileUrl = profile.getUrl();
                Picasso.with(mContext)
                        .load(profileUrl)
                        .config(Bitmap.Config.RGB_565)
                        .error(R.drawable.ic_load_error)
                        .into(viewHolder.userProfile);
            }
            viewHolder.userProfile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    OtherUserInfoActivity.startActivity((Activity) mContext, mData.get(position).getAuthor().getObjectId());
                }
            });
        }
        viewHolder.time.setText(mData.get(position).getCreatedAt());
        if (mData.get(position).getContent() != null && !mData.get(position).getContent().isEmpty()) {
            viewHolder.content.setText(mData.get(position).getContent());
        } else {
            viewHolder.content.setVisibility(View.GONE);
        }
        if (mData.get(position).getImages() != null && mData.get(position).getImages().size() > 0) {
            final List<String> images = new ArrayList<>();
            for (BmobFile file : mData.get(position).getImages()) {
                images.add(file.getUrl());
            }
            NetImageGridAdapter adapter = new NetImageGridAdapter(mContext, images);
            switch (mData.get(position).getImages().size()) {
                case 1:
                    viewHolder.imageGridView.setLayoutManager(new LinearLayoutManager(mContext));
                    break;
                case 2:
                case 4:
                    viewHolder.imageGridView.setLayoutManager(new GridLayoutManager(mContext, 2));
                    break;
                default:
                    viewHolder.imageGridView.setLayoutManager(new GridLayoutManager(mContext, 3));
                    break;
            }
            viewHolder.imageGridView.setAdapter(adapter);
            viewHolder.imageGridView.setVisibility(View.VISIBLE);
        } else {
            viewHolder.imageGridView.setVisibility(View.GONE);
        }
    }

    private static class ItemViewHolder extends AbsViewHolder {

        ImageView userProfile;
        TextView author;
        TextView time;
        TextView content;
        RecyclerView imageGridView;

        ItemViewHolder(View itemView) {
            super(itemView);
            userProfile = (ImageView) itemView.findViewById(R.id.id_follow_user_profile);
            author = (TextView) itemView.findViewById(R.id.id_post_author);
            time = (TextView) itemView.findViewById(R.id.id_post_time);
            content = (TextView) itemView.findViewById(R.id.id_post_content);
            imageGridView = (RecyclerView) itemView.findViewById(R.id.id_image_grid_view);
        }
    }

}
